﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Caliburn.Core.InversionOfControl;
using Caliburn.PresentationFramework;
using Caliburn.PresentationFramework.RoutedMessaging;
using Caliburn.PresentationFramework.Screens;
using Caliburn.ShellFramework.Results;
using MTGenome.MTG;

namespace MTGenome.ViewModels
{
    [Singleton(typeof(GameSurfaceViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class GameSurfaceViewModel : RescueBase
    {
        private readonly DialogViewModel _dialog;
        private readonly IDeckLoader _deckLoader;
        private readonly ILibrary _library;

        public GameSurfaceViewModel(DialogViewModel dialog, IDeckLoader deckLoader, ILibrary library)
        {
            _dialog = dialog;
            _deckLoader = deckLoader;
            _library = library;

            DisplayName = "Game Surface";
            CardsInPlay = new BindableCollection<CardInPlayViewModel>();
        }

        public BindableCollection<CardInPlayViewModel> CardsInPlay
        {
            get;
            private set;
        }

        public IEnumerable<IResult> Open()
        {
            yield return Show.CommonDialog(_dialog.OpenFile);

            Open(_dialog.OpenFile.FileName);
        }

        private void Open(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                IDeck deck = _deckLoader.Load(path);
                CardsInPlay.Clear();
                List<ICard> cards = new List<ICard>();

                using (IDatabase database = IoC.Get<IDatabase>())
                {
                    foreach (var cardQty in deck.Cards)
                    {
                        ICard card = database.Cards.FirstOrDefault(c => c.MultiverseID == cardQty.MultiverseID);
                        if (card != null)
                        {
                            for (int i = 0; i < cardQty.Quantity; i++)
                            {
                                cards.Add(card);
                            }
                        }
                    }
                }

                _library.Load(cards);
                Reset();
            }
        }

        public void Draw()
        {
            CardsInPlay.Add(IoC.Get<CardInPlayViewModel>().WithSubject(_library.Draw()) as CardInPlayViewModel);
        }

        public void Shuffle()
        {
            _library.Shuffle();
        }

        public void Reset()
        {
            foreach (var item in CardsInPlay)
            {
                _library.PlaceOnTop(item.Subject);
            }
            CardsInPlay.Clear();
            _library.Shuffle();

            for (int i = 0; i < 7; i++)
            {
                Draw();
            }
        }

        public void Untap()
        {
            foreach (CardInPlayViewModel item in CardsInPlay)
            {
                item.Tapped = false;
            }
        }
    }
}
